package com.jeremyfeinstein;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by sunxunchao on 2018/9/26.
 */

public abstract class OnCustomTouchListener implements View.OnTouchListener {

    private int count = 0;//点击次数
    private long firstClick = 0;//第一次点击时间
    private long secondClick = 0;//第二次点击时间
    /**
     * 两次点击时间间隔，单位毫秒
     */
    private final int totalTime = 300;

    private float startRawY;
    private float slidDistance;

    private static final String TAG = "OnCustomTouchListener";

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        boolean isTouch = false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startRawY = event.getRawY();
                Log.d(TAG, "onTouch: ACTION_DOWN  Y:" + event.getY() + "--event.getRawY " + event.getRawY());
                isTouch = onDoubleEvent(v, event);
                break;
            case MotionEvent.ACTION_MOVE:
                float moveRawY = event.getRawY();
                slidDistance = moveRawY - startRawY;
                if (slidDistance > 200) {
                    //向下滑动
                    Log.d(TAG, "onTouch: 向下滑动" + slidDistance);
                    onSlidDown(v);
                    isTouch = true;
                } else if (slidDistance < -200) {
                    //向上滑动
                    Log.d(TAG, "onTouch: 向上滑动" + slidDistance);
                    onSlidUp(v);
                    isTouch = true;
                }
                Log.d(TAG, "slidDistance: " + slidDistance);
                Log.d(TAG, "onTouch: ACTION_MOVE  Y:" + event.getY() + "--event.getRawY " + event.getRawY());
                break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG, "onTouch: ACTION_UP  Y:" + event.getY() + "--event.getRawY " + event.getRawY());
                float endRawY = event.getRawY();
                slidDistance = endRawY - startRawY;
//                if (slidDistance <= 200 && slidDistance >= -200) {
//                onClicked(v);
//                isTouch = true;
//                }
                break;
        }
        return isTouch;
    }


    private boolean onDoubleEvent(View view, MotionEvent event) {
        boolean isTouch = false;
        if (MotionEvent.ACTION_DOWN == event.getAction()) {//按下
            count++;
            if (1 == count) {
                firstClick = System.currentTimeMillis();//记录第一次点击时间
            } else if (2 == count) {
                secondClick = System.currentTimeMillis();//记录第二次点击时间
                if (secondClick - firstClick < totalTime) {//判断二次点击时间间隔是否在设定的间隔时间之内
                    onDoubleClicked(view);
                    count = 0;
                    firstClick = 0;
                    isTouch = true;
                } else {
                    firstClick = secondClick;
                    count = 1;
                }
                secondClick = 0;
            }
        }
        onClicked(view);
        return isTouch;
    }

    protected abstract void onDoubleClicked(View view);

    protected abstract void onClicked(View view);

    protected abstract void onSlidUp(View view);

    protected abstract void onSlidDown(View view);
}

